<template>
  <en-table-layout
    :tableData="tableData.data"
    :loading="loading"
    :tips="true">
    <div slot="toolbar" class="inner-toolbar">
      <div class="toolbar-btns">
        <el-button size="mini" type="primary" @click="handleFalsifyRecordScan">手动触发扫描</el-button>
      </div>
      <div class="toolbar-search">
        <en-table-search
          @search="searchEvent"
          @advancedSearch="advancedSearchEvent"
          advanced
          advancedWidth="465"
          placeholder="请输入模块名称"
        >
          <template slot="advanced-content">
            <el-form ref="advancedForm" :model="advancedForm" label-width="80px">
              <el-form-item label="状态">
                <el-select v-model="advancedForm.state" placeholder="请选择" clearable>
                  <el-option label="已修复" value="repaired"/>
                  <el-option label="未修复" value="unrepaired"/>
                </el-select>
              </el-form-item>
            </el-form>
          </template>
        </en-table-search>
      </div>
    </div>
    <template slot="table-columns">
      <!--模块-->
      <el-table-column prop="module" label="模块"/>
      <!--模块ID-->
      <el-table-column prop="data_id" label="模块ID"/>
      <!--状态-->
      <el-table-column label="状态">
        <template slot-scope="scope">{{ scope.row.state === 'unrepaired' ? '未修复' : '已修复' }}</template>
      </el-table-column>
      <!--操作-->
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state === 'unrepaired'"
            size="mini"
            type="primary"
            @click="handleRepaired(scope.row)">修复</el-button>
        </template>
      </el-table-column>
    </template>
    <el-pagination
      v-if="tableData"
      slot="pagination"
      @size-change="handlePageSizeChange"
      @current-change="handlePageCurrentChange"
      :current-page="tableData.page_no"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="tableData.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.data_total">
    </el-pagination>
  </en-table-layout>
</template>

<script>
import * as API_System from '@/api/system'

export default {
  name: 'orderList',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 列表数据
      tableData: '',
      // 高级搜索数据
      advancedForm: {}
    }
  },
  mounted() {
    this.GET_FalsifyRecord()
  },
  methods: {
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_FalsifyRecord()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_FalsifyRecord()
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        module: data
      }
      Object.keys(this.advancedForm).forEach(key => delete this.params[key])
      this.params.page_no = 1
      this.GET_FalsifyRecord()
    },

    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      delete this.params.module
      this.params.page_no = 1
      this.GET_FalsifyRecord()
    },

    /** 手动触发扫描 */
    handleFalsifyRecordScan() {
      this.$confirm('触发扫描会占用服务器资源，确定要触发吗？', '提示', { type: 'warning' }).then(() => {
        API_System.getFalsifyRecordScan().then(() => {
          this.GET_FalsifyRecord()
        })
      }).catch(() => {})
    },

    /** 修复篡改记录 */
    handleRepaired(row) {
      this.$confirm('修复操作会重置签名，需要您确保数据已经修复正常，确认数据已经修复吗？', '提示', { type: 'warning' }).then(() => {
        API_System.editFalsifyRecord(row.record_id).then(() => {
          this.$message.success('修复成功')
          this.GET_FalsifyRecord()
        })
      }).catch(() => {})
    },

    /** 获取篡改记录列表 */
    GET_FalsifyRecord() {
      this.loading = true
      const params = this.MixinClone(this.params)
      API_System.getFalsifyRecord(params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
/deep/ .el-table td:not(.is-left) {
  text-align: center;
}

.inner-toolbar {
  display: flex;
  width: 100%;
  justify-content: space-between;
  padding: 0 20px;
}

.goods-image {
  width: 50px;
  height: 50px;
}

.two-step-input {
  margin-top: 20px;
}
</style>

